<?php 


/**
 * Base class for the commands which use data base connection 
 * @author leo
 *
 */
abstract class Shell_Command_Abstract_CommandBase  
{
		
		protected $NAME = '*';
		
		/** An instance of Zend_Console_Getopt. Meant for manupulating the command flags and options. */
		protected $options;
		
		/*const OPT_OK = "opt_ok";
		const OPT_FAIL = "opt_fail";
		const OPT_STOP = "opt_stop";*/
		
//*************************************************************************************************
        public function __construct()
        {
           	$optConfig = array( 'help' => 'Display help for the command.');
        	$this->options = new Zend_Console_Getopt( $optConfig );
        	
        }
        
        
//*************************************************************************************************
        public function execute($arguments)
        {
        	$this->options->setArguments($arguments);        	
        }

//*************************************************************************************************
		protected function parseOptions()
		{
		    try
			{
				//echo $this->options->toString() . "\n";
				$this->options->parse();
				
				if ($this->options->help)
				{
					$this->displayHelp($this->NAME);
					return false; //return self::OPT_STOP;
				}
			}
			catch (Zend_Console_Getopt_Exception $e)
			{
				$usage = $e->getUsageMessage()."\n";
				$progname = $_SERVER["SCRIPT_NAME"];
				$usage = str_replace($progname, $this->getName(), $usage);
				echo "\n\n" . $usage . "\n\n";
				return false; //return self::OPT_FAIL;
			}
			
			return true; //return self::OPT_OK;
		}
        
        
//*************************************************************************************************
/** Tries to display a help file for a specified topic */
		protected function displayHelp($topic)
		{
			//TODO: probably this path should not be hard-coded like this.
			$helpPath = getcwd() . "/shell/data/";
			
			if ($helpText = @file_get_contents($helpPath.'help_'.$topic.'.txt') )
				echo $helpText . "\n\n";
			else
				echo "\nHelp: no help for $topic found!\n\n";
		}
//*************************************************************************************************
		public function getName()
		{
			return $this->NAME;
		}
	
	/*public function execute($arguments)
	{
		echo "must be overriden!";	
	}*/
//*************************************************************************************************
		/**
		 * 
		 * Validates error code. Prints either positive or negative message depending on the code
		 * @param unknown_type $code Currently if code is zero, then positive msg is displayed, otherwise -- negative
		 * @param unknown_type $positiveMsg Is displayed if error code is zero (==0). 
		 * @param unknown_type $negativeMsg Is displayed if error code is not zero (!=0)
		 */
		protected function parseErrorCode($errorCode, $positiveMsg, $negativeMsg)
		{			
			if ($errorCode == Core_Enum_SQLiteErrorCodes::SQLITE_OK)
			{
				echo "\n$positiveMsg\n\n";
			}
			
			else
			{
				echo "\n$negativeMsg\n\n";
			}			
		}

//*************************************************************************************************
		public function __toString()
		{
			$usage = $this->options->getUsageMessage()."\n";
			$progname = $_SERVER["SCRIPT_NAME"];
			$usage = str_replace($progname, $this->getName(), $usage);			
			return $usage;
		}
		
}